

var router = require('express').Router();
var articleService = require("../service/article-service");


router.get("/pages/article/:id", async (request, response) => {
    const { id } = request.params;

    const article = await articleService.getArticleByID(parseInt(id));   
    if (!article)
    {
        response.status(404).render("message", {
            title: "404",
            message: "Sorry can't find that!"
        });
        return;
    }

    if (article.content instanceof Array)
        article.content = articleService.articleContentJSON2HTML(article.content);
    
    article.title = article.title.replaceAll("<", "&lt;");
    const documentTitle = `${article.sort_name}——${article.title}`;
    const documentDescription = `尘跃的个人分享网——文章——${article.author}——${documentTitle}`;
    const html = `
        <!DOCTYPE html>
        <html lang="zh">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
        
            <!-- 信息 -->
            <link rel="icon" href="/favicon.ico" type="image/x-icon" />
            <title>${documentTitle}</title>
            <meta name="author" content="${article.author}" />
            <meta
                name="description"
                content="${documentDescription}"
            />
        
            <!-- Facebook Open Graph -->
            <meta property="og:title" content="${documentTitle}" />
            <meta property="og:type" content="article">
            <meta property="og:url" content="http://cyue.net/pages/article/${article.id}">
            <meta property="og:image" content="http://cyue.net/favicon.png" />
            <meta property="og:site_name" content="尘跃的个人分享网" />
            <meta
                property="og:description" 
                content="${documentDescription}"
            />
        
            <!-- Twitter Cards -->
            <meta name=twitter:card content="summary" />
            <meta name=twitter:image content="http://cyue.net/favicon.png" />
            <meta name="twitter:title" content="${documentTitle}" />
            <meta name=twitter:description content="${documentDescription}" />
            <meta name="twitter:creator" content="@${article.author}" />
            <meta name="twitter:site" content="@cyue.net" />
        
        
            <link rel="stylesheet" href="/css/style.css">
            <link rel="stylesheet" href="/css/style-fit.css">
            <link rel="stylesheet" href="/css/article.css">
        
            <!-- 百度统计 -->
            <script src="/js/tongji.js"></script>
            <!-- Vue3 -->
            <script src="/js/vue/vue.global.prod.js"></script>
            <!-- WX JS SDK -->
            <script src="/js/wx.js"></script>
        </head>
        <body>
            <my-header></my-header>
            
        
            <div class="block-spacer"></div>
            <div class="block-spacer"></div>
            <main>
                <article>
                    <h1 class="article-title">${article.title}</h1>
                    <div class="article-author-container">
                        <span>作者：</span>
                        <a class="article-author" href="#">${article.author}</a>
                    </div>
                    <h4 class="article-date">${article.date}</h4>
        
                    <!-- <div class="block-spacer"></div> -->
                    <hr/>
        
                    ${article.content}
                </article>
            </main>
        
        
            <my-footer></my-footer>
        
            <script src="/js/third/axios.min.js"></script>
            <script src="/js/http.js"></script>
            <script src="/js/app.js"></script>
            <script src="/js/style.js"></script>
            <script src="/js/android.js"></script>
        </body>
        </html>
        
    
    `;
    response.send(html);
});



module.exports = router;

